package com.web;

import com.alibaba.fastjson.JSONArray;
import com.bean.Subsidy;
import com.service.ISubsidyManagementService;
import com.service.SubsidyManagementServiceImp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/SubsidyAddServlet")
public class SubsidyAddServlet extends HttpServlet {

    private ISubsidyManagementService subsidyManagementService = new SubsidyManagementServiceImp();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Subsidy subsidy = new Subsidy(
                null,
                request.getParameter("subsidy_project_name"),
                Integer.parseInt(request.getParameter("subsidy_subject_id")),
                Integer.parseInt(request.getParameter("countryside_department_id")),
                Integer.parseInt(request.getParameter("city_department_id")),
                0

        );

        //判断是否有最后一个参数来判断是否有启用标记
        if(request.getParameter("flag")!=null)
            subsidy.setFlag(true);

        System.out.println(subsidy);

        boolean result = subsidyManagementService.addSubsidy(subsidy);
        String jsonResult = JSONArray.toJSONString(result);
        response.getWriter().write(jsonResult);
    }
}
